﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using QDSpider.database;
using QDSpider.Models;

namespace QDSpider.manager
{
    public static class UserManager
    {
        public static string Md5(string s)
        {
            var b = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(s));
            return b.Aggregate("", (current, t) => current + t.ToString("x").PadLeft(2, '0'));
        }
        public static User Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return null;
            try
            {
                using (var db = new S002Database())
                {
                    password = Md5(password);
                    var user =
                        db.SingleOrDefault<User>("select  * from vIFO_Users where username=@0 and userpassword=@1",
                            username, password);
                    return user ;
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
    }
}
